package wojtek.ckm;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JList;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class LigaGUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
 boolean widac = true;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Liga liga1 = new Liga();
					LigaGUI frame = new LigaGUI(liga1);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LigaGUI(final Liga liga1) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblNazwaKlubu = new JLabel("nazwa klubu");
		contentPane.add(lblNazwaKlubu, "6, 2, right, default");
		
		textField = new JTextField();
		contentPane.add(textField, "8, 2, fill, default");
		textField.setColumns(10);
		
		JLabel lblBarwy = new JLabel("barwy");
		contentPane.add(lblBarwy, "6, 4, right, default");
		
		textField_1 = new JTextField();
		contentPane.add(textField_1, "8, 4, fill, default");
		textField_1.setColumns(10);
		
		final JList list = new JList();
		contentPane.add(list, "8, 6, fill, fill");
		list.addListSelectionListener( new ListSelectionListener(){

			@Override
			public void valueChanged(ListSelectionEvent e ) {
if (e.getValueIsAdjusting() == false){

	setWidac(true);
	btnEdytujKlub.setVisible(false);
	btnEdytujKlub.setEnabled(false);

	
	int numer = list.getSelectedIndex();
	textField.setText(liga1.zwrocKlub(numer).getNazwaKlubu());
	textField_1.setText(liga1.zwrocKlub(numer).getBarwy());
}
			}
		});
		
		
		final JButton btnDodajKlub = new JButton("Dodaj klub");
		contentPane.add(btnDodajKlub, "6, 8");
		
		btnDodajKlub.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("asdas");
						if (getTextField().getText().length()<=0){
											
							System.out.println("Nie wpisales nazwy klubu");

						}
						else{
							dodajKlubik(liga1);
							list.setListData(liga1.getKluby());
						}

			}
		});
		


		JButton btnEdytujKlub = new JButton("Edytuj klub");
		contentPane.add(btnEdytujKlub, "8, 8");
		btnEdytujKlub.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
		if(liga1.zwrocKlub(list.getSelectedIndex()) != null){
edytujKlub(liga1.zwrocKlub(list.getSelectedIndex()));
		}

			}});
	
		
		
		JButton btnUsunKlub = new JButton("Usun klub");
		contentPane.add(btnUsunKlub, "10, 8");
		btnUsunKlub.setVisible(widac);
		btnUsunKlub.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
	
		usunKlubik(liga1, list);
		list.setListData(liga1.getKluby());

			}});
	}

	public void dodajKlubik(Liga liga1){
		Klub klub = new Klub(textField.getText(), textField_1.getText());
		liga1.dodajklub(klub);
	}
	
	
	
	public void usunKlubik(Liga liga1, JList list){
		
		if(liga1.zwrocKlub(list.getSelectedIndex()) != null){
			
		System.out.println(list.getSelectedIndex());

		int usuwanyKlub = list.getSelectedIndex();
		liga1.usunKlub(usuwanyKlub);
		}
		else {
			System.out.println("nie zaznaczyles zadnnego klubu");
		}
		
		}
		

	public void edytujKlub(final Klub klub){
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						KlubGUI frame = new KlubGUI(klub);
						frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}

	public JTextField getTextField() {
		return textField;
	}

	public void setTextField(JTextField textField) {
		this.textField = textField;
	}

	public JTextField getTextField_1() {
		return textField_1;
	}

	public void setTextField_1(JTextField textField_1) {
		this.textField_1 = textField_1;
	}

	public boolean isWidac() {
		return widac;
	}

	public void setWidac(boolean widac) {
		this.widac = widac;
	}
}
